package BanyuanClub.Study.Day0301;

import java.nio.channels.Pipe;
import java.util.Date;

public class MyLinkedList {

    private Node first;
    private int size;

    //添加到头部
    public void addFrist(Object obj){
        Node head = new Node();
        head.date = obj;
        head.nest = first;
        first = head;
        size++;
    }

    //添加到链表尾部
    public void addList(Object obj){
        Node node = new Node();
        node.date = obj;
        if (first == null){
            first = node;
            size++;
            return;
        }
        Node temp = first;
        while (temp != null){
            temp = temp.nest;
        }
        temp.nest = node;
        size++;
    }

    //删除头部
    public void removeFirst(){
        if (first == null){
            throw new IllegalStateException();
        }
        Node temp = first.nest;
        first.date = null;
        first.nest = null;
        first = temp;
    }

    //删除尾部
    public void removeList(){
        if (first == null){
            throw new IllegalStateException();
        }
        if (first.nest == null){
            first.date = null;
            return;
        }
        Node temp = first;
        while (temp.nest.nest != null){
            temp = temp.nest;
        }
        temp.nest.date = null;
        temp.nest = null;
    }

    //查询指定位置的元素
    public Object get (int index) {
        if (index < 0 || index >= size){
            return null;
        }
        if (size == 0){
            return null;
        }
        Node temp = first;
        for (int i = 0; i < index; i++) {
            temp = temp.nest;
        }
        return temp.date;
    }

    public int size(){
        return size;
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addFrist("aaa");
        myLinkedList.addFrist("bbb");
        myLinkedList.addFrist("ccc");
        myLinkedList.addFrist("www");
        for (int i = 0; i < myLinkedList.size() ; i++) {
            System.out.println(myLinkedList.get(i));
        }
    }

}



class Node{
    public Object date;
    public Node nest;
}